//-----------------------------------------------------------------------------
//  File          : emv2dll.h
//  Module        : 
//  Description   : Header for EMV Kernel Export functions.
//  Author        : Lewis
//  Notes         :
//
//  Date          Who         Action
//  ------------  ----------- -------------------------------------------------
//  15 Jan  2009  Lewis       Initial Version. V1.0
//  09 Feb  2009  Lewis       Inc max appl support to 128. SubVer:1
//-----------------------------------------------------------------------------
#ifndef _EMV2DLL_H_
#define _EMV2DLL_H_
#include "common.h"

//-----------------------------------------------------------------------------
//      Defines
//-----------------------------------------------------------------------------
#define EMVDLL_ID                       0x0E

typedef struct {
  WORD wLen;
  BYTE *bMsg;
} sIO_t;

// 09-JUN-2005 AK
#define MAX_APPL        128             /* max nbr of Appl allowed */
#define MAX_SEL         8               /* max nbr of selection */
#define APPL_TLEN       200             /* Appl config area */
#define CLIP_TLEN       4000            /* work area */  // !2008-05-23 V4.1e 2CC.144.02

typedef unsigned char   TL;
#define TLMax           0xFF            /* 0xFFFE if TL is WORD */

typedef struct {                        /* end indicator = 0 Tag */
  WORD wTag;
  TL   sLen;
  BYTE *pbLoc;
} TLIST;                                /* tag list */

typedef struct {
  DWORD Exponent;
  DWORD KeySize;
  BYTE  Key[256];
} RSA_KEY;

typedef struct {
  BYTE  KeyIdx;
  BYTE  RID[5];
  RSA_KEY CAKey;
  WORD  ExpiryDate;     // in MMYY
  WORD  EffectDate;     // in MMYY
  WORD  Chksum;
} KEY_ROOM;

typedef struct {
  BYTE  KeyIdx;
  BYTE  RID[5];
  WORD  ExpiryDate;     // in MMYY
  WORD  EffectDate;     // in MMYY
} KEY_INFO;


// EMV Application Type
#define TYPE_EMV                1
#define TYPE_VSDC               2
#define TYPE_MCHIP              3
#define TYPE_JSMART             4
#define TYPE_PBOC               5 

// Bit Control Field Defines
#define bit_eASI        	0x01
#define bit_eReferral   	0x02
#define bit_ePinBypass  	0x04
#define bit_eForceTRM   	0x08
#define bit_eAdvice       0x10
// !Lewis No Use ++
//#define bit_eTACDenExist	0x10
//#define bit_eTACOnlExist	0x20
//#define bit_eTACDefExist	0x40
// !Lewis No Use --
#define bit_eMultiAcq   	0x80

#define eNO_PARTIAL_AID    bit_eASI
#define eNO_REFERRAL       bit_eReferral
#define eENB_PIN_BYPASS    bit_ePinBypass
#define eFORCE_TRM         bit_eForceTRM
#define eENB_ADVICE        bit_eAdvice
// !Lewis No Use ++
//#define eTACDEN_EXIST      bit_eTACDenExist
//#define eTACONL_EXIST      bit_eTACOnlExist
//#define eTACDEF_EXIST      bit_eTACDefExist
// !Lewis No Use --
#define eMULTI_ACQ         bit_eMultiAcq


#pragma pack(1)
// 21-3-2007 V02 R1
typedef struct {
  BYTE eType;                           /* 0: not in use; 1:EMV; 2: VSDC */
  BYTE eBitField;                       /* Bit Control Field */
  DWORD eRSBThresh;                     /* Threshold for Biased Random Sel */
  BYTE eRSTarget;                       /* Target for Random Sel */
  BYTE eRSBMax;                         /* Max target for Biased Random Sel */
  BYTE eTACDenial[5];
  BYTE eTACOnline[5];
  BYTE eTACDefault[5];
  BYTE eACFG[APPL_TLEN];
} APP_CFG;
#pragma pack()

//HQ, 6-Sep-2007, V3.0
//for certification revocation list support
#define BIT_PSE_SUPPORT		0x80
#define BIT_DISP_RANDOM		0x40

#define MAX_RID			3
#define MAX_CRL_PER_RID		30

#define CRL_SETUP_OK		0
#define CRL_ERR_LENGTH		1
#define CRL_ERR_RID_FULL	2
#define CRL_ERR_CRL_FULL	3
#define CRL_NOT_FOUND		4
#define CRL_EXIST		5

typedef struct {
  BYTE rid[5];
  BYTE cert_idx;
  BYTE cert_sn[3];
} CERTREVOC_LIST;


// DllSetup Command ID
#define IOCMD_IFDSN_RW              1
#define IOCMD_KEY_LOADING           2
#define IOCMD_CLR_AID               3
#define IOCMD_LOAD_AID              4
#define IOCMD_CLOSE_LOAD            5
#define IOCMD_ADD_AID               6
#define IOCMD_DEL_AID               7
#define IOCMD_READ_AID              8
#define IOCMD_READ_AIDIDX           9
#define IOCMD_READ_KEY              10
#define IOCMD_CONFIG_STATUS         11
#define IOCMD_DNLOAD_PARAM          12
#define IOCMD_SET_KEY_VERSION       13
#define IOCMD_DEL_MULTI_ACQ         14
#define IOCMD_READ_MULTI_ACQ        15
#define IOCMD_READ_CHKSUM           16
#define IOCMD_CLR_CRL			          17
#define IOCMD_ADD_CRL			          18
#define IOCMD_DEL_CRL			          19
#define IOCMD_SET_PINPAD		        20

// DllMsg Command ID
#define IOMSG_PROC_INIT             0
#define IOMSG_PROC_START            1
#define IOMSG_ENTER_SEL             2
#define IOMSG_ONL_PIN               3
#define IOMSG_ONL_RESULT            4
#define IOMSG_VOICE_REFER           5
#define IOMSG_AMT_IAP               6
#define IOMSG_VALID_CARD            7
#define IOMSG_ENTER_AMT             8
#define IOMSG_RESTART               9

//-----------------------------------------------------------------------------
//   Kernel Support Transaction Types
//-----------------------------------------------------------------------------
#define EGOOD_SALE        0
#define EVOID             1
#define EREFUND           2
#define EADJUST           3
#define ESERVICE_SALE     4
#define ECASH_WDRAW       5
#define EGS_WCASHBACK     6
#define ESS_WCASHBACK     7
#define EAUTH             8

// Transaction Control  
// ERD_CARDLOG is PBOC function.
#define EFORCE_ONL        0x40
#define EPIN_BYPASS       0x20
#define EREAD_CARDLOG     0x10          

//-----------------------------------------------------------------------------
//   Export Functions
//-----------------------------------------------------------------------------
enum {
  EMV_DLLSETUP                , // 01
  EMV_DLLMSG                  , // 02
  EMV_PACKREC                 , // 03
  EMV_FINDREC                 , // 04
  EMV_SETAUTOPIN              , // 05  DEBUG_ONLY
  EMV_FUNC_COUNT                // Must be the last item
};
//extern BOOLEAN dllSetup(DWORD bEntry, sIO_t* sio_in, sIO_t* sio_out);
#define EMV_DllSetup(x,y,z)    (BOOLEAN) lib_app_call(EMVDLL_ID, EMV_DLLSETUP, (DWORD)x, (DWORD)y, (DWORD)z)
//extern BOOLEAN dllMsg(DWORD bEntry, sIO_t * pin, sIO_t * pout);
#define EMV_DllMsg(x,y,z)      (BOOLEAN) lib_app_call(EMVDLL_ID, EMV_DLLMSG, (DWORD)x, (DWORD)y, (DWORD)z)
//extern BYTE * ePackRec(BYTE * pbPtr, BYTE *pout, WORD *pTag);
#define EMV_PackRec(x,y,z)    (BYTE *) lib_app_call(EMVDLL_ID, EMV_PACKREC, (DWORD)x, (DWORD)y, (DWORD)z)
//extern BYTE * eFindRec(BYTE *pbPtr, WORD wTag, WORD *size)
#define EMV_FindRec(x,y,z)    (BYTE *) lib_app_call(EMVDLL_ID, EMV_FINDREC, (DWORD)x, (DWORD)y, (DWORD)z)
//extern void SetAutoPin(int aPin); // !DEBUG_ONLY
#define EMV_SetAutoPin(x)     lib_app_call(EMVDLL_ID, EMV_SETAUTOPIN, (DWORD)x, 0, 0)

// EMV Process Error Code
#define ERR_CONFIG              0x80
#define ERR_MEMORY              0x81
#define ERR_SMCIO               0x82
#define ERR_DATA                0x83
#define ERR_CANCEL              0x84
#define ERR_NOAPPL              0x85    /* no matching application */
#define ERR_NOMORE              0x86    /* no more application */
#define ERR_BLOCKED             0x87    /* card blocked */
#define ERR_CARDL1              0x88    /* level 1 card */
#define ERR_RESTART             0x89    /* card restart failure 09-JUN-2005 AK */
#define ERR_SEQ                 0x90    /* command sequence */
#define ERR_SETUP               0x91    /* parameter not loaded */
#define ERR_KEY                 0x92    /* key error */
#define ERR_NOLOG               0x93    /* no transaction log on card */ // 4-JAN-2006 Ray ++
#define ERR_NOTMATCH		        0x94	  //HQ, 19-Sep-2006, 2CL.127.01, 2CL.134.01.01/02/03

// Action Code
#define ACT_SIGNATURE           0x01    /* signature */
#define ACT_DDAAC               0x02    /* Combine DDA/AC */
#define ACT_2AC                 0x04    /* Second AC */
#define ACT_DEFCVM              0x08    /* Default CVM */
#define ACT_ONLINE              0x10    /* Online processing */
#define ACT_REFERRAL            0x20    /* Referral */
#define ACT_HOSTAPP             0x40    /* Host Approved */
#define ACT_APPROVED            0x80    /* Trans Approved */

// Private Tag
#define TagIFLimit              0xDF7F  // IFloorLimit 09-JUN-2005 AK
#define TAG_BITCTRL             0xDF7E  // Bit Control Field !2008-11-20

// Max EMV IO buffer size
#define EMV_MAX_BUFSIZE         3000
#endif // _EMV2DLL_H_
